package com.woniuxy.student.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.woniuxy.commons.entity.ReplySheet;
import com.woniuxy.student.dao.ReplySheetDao;
import com.woniuxy.student.service.ReplySheetService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 曾令忠
 * @Description:
 * @Date: 2020/7/25 0025-下午 4:52
 */
@Service
public class ReplySheetServiceImpl implements ReplySheetService {
    @Resource
    ReplySheetDao replySheetDao;

    /**
     * 批量插入学生答案
     * @param id id
     * @param replies
     */
    @Override
    @Transactional
    public void insertReply(int id, String replies) {
        List<ReplySheet> replySheets = new ArrayList<>();
        JSONArray array = JSONArray.parseArray(replies);
        try {
            for (int i = 0; i <array.size() ; i++) {
                ReplySheet replySheet = new ReplySheet();
                replySheet.setReplySheetId(id);
                replySheet.setReplyQuestionId((int)array.getJSONObject(i).get("parentnum"));
                replySheet.setReplySonId((int)array.getJSONObject(i).get("chidnum"));
                replySheet.setReplyAnswer((String) array.getJSONObject(i).get("sanswer"));
                replySheets.add(replySheet);
            }
            replySheetDao.insertReply(replySheets);
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }

    }
}
